/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.reddys.rad.inventory.service;

import com.reddys.rad.inventory.db.Product;
import com.reddys.rad.inventory.db.ProductCategory;
import com.reddys.rad.inventory.db.PurchaseOrder;
import com.reddys.rad.inventory.db.PurchaseOrderDetails;
import com.reddys.rad.inventory.db.Sale;
import com.reddys.rad.inventory.db.SaleDetails;
import com.reddys.rad.inventory.db.Stock;
import com.reddys.rad.inventory.db.StockDetails;
import com.reddys.rad.inventory.db.Vendor;
import static com.reddys.rad.inventory.service.PurchaseOrderService.em;
import com.reddys.rad.inventory.utils.EntityManagerImpl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 *
 * @author B V Dinesh
 */
public class SalesService {
    
   static EntityManager em;

    public static SaleDetails save(SaleDetails saleDetails){
        saleDetails = (SaleDetails) EntityManagerImpl.persist(saleDetails);
        return saleDetails;
    }
    
   public static Sale saveHeader(Sale sale){
        sale = (Sale) EntityManagerImpl.persist(sale);
        return sale;
    }
   public static StockDetails saveStockDetails(StockDetails stockDetails){
        stockDetails = (StockDetails) EntityManagerImpl.persist(stockDetails);
        return stockDetails;
    }
    
   public static Stock updateStock(Stock stock){
        stock = (Stock) EntityManagerImpl.merge(stock);
        return stock;
    }

    public static ObservableList getProductList() {
         ObservableList data = FXCollections.observableArrayList();
        em = EntityManagerImpl.getEntityManager();
        Query query = em.createNamedQuery("Product.findAll");
        List productList = query.getResultList();
        Iterator iter = productList.iterator();
        while(iter.hasNext()){
            Product product = (Product) iter.next();
            data.add(product.getProductId());
        }
        return data;
    }
    
     public static ObservableList getVendorList() {
         ObservableList data = FXCollections.observableArrayList();
        em = EntityManagerImpl.getEntityManager();
        Query query = em.createNamedQuery("Vendor.findByVendortype");
        query.setParameter("vendortype", "Customer");
        List vendorList = query.getResultList();
        Iterator iter = vendorList.iterator();
        while(iter.hasNext()){
            Vendor vendor = (Vendor) iter.next();
            data.add(vendor);
            System.out.println("data"+data);
            System.out.println("vendor"+vendor);
            
        }
        return data;
    }
     
    public static Stock getStock(String productId) {
         ArrayList<Stock> stockList = new ArrayList<Stock>();
         Stock stock = null;
        em = EntityManagerImpl.getEntityManager();
        Query query = em.createNamedQuery("Stock.findByProductId");
        query.setParameter("productId",productId);
        List list = query.getResultList();
        Iterator iter = list.iterator();
        while(iter.hasNext()){
            stock = (Stock) iter.next();
           // stockList.add(stock);
        }
        return stock;
    }
    
    public static ProductCategory getProductCategoryByDesc(String categoryDesc) {
        em = EntityManagerImpl.getEntityManager();
        ProductCategory category = null;
        Query query = em.createNamedQuery("ProductCategory.findByProductCategoryDescription");
        query.setParameter("productCategoryDescription", categoryDesc);
        List categoryList = query.getResultList();
        Iterator iter = categoryList.iterator();
        while(iter.hasNext()){
            category = (ProductCategory) iter.next();
        }
        return category;
    }
    
}
